﻿using System;
using System.IO;
using System.Runtime.Serialization;

namespace _038_不同的类型使用相同的数据协定
{
    class Program
    {
        static void Main(string[] args)
        {

            using (FileStream 表1实例化=new FileStream("test.xml",FileMode.Create))
            {
                学生表1 操作之1 = new 学生表1
                {
                    ID = 201811428023,
                    Name = "汪洋大哥",
                    Email = "web@z01.com"
                };
                DataContractSerializer 输出来 = new DataContractSerializer(操作之1.GetType());
                输出来.WriteObject(表1实例化, 操作之1);

                using(StreamReader 读取=new StreamReader("test.xml"))
                {
                    Console.WriteLine("表1的数据：\n");
                    Console.WriteLine(读取.ReadToEnd());
                }
            }


            //从该XML中读取数据，进行反序列化，但接收类型是表2
            using (FileStream 表2反序列实例化 = new FileStream("test.xml", FileMode.Open))
            {
                DataContractSerializer 操作之 = new DataContractSerializer(typeof(学生表2));
                学生表2 操作之2 = (学生表2)操作之.ReadObject(表2反序列实例化);

                string 输出之 = "\n\n序列化后输出的信息状态：\n" +
                    $"学生姓名：{操作之2.StudentName}\n" +
                    $"学生编号：{操作之2.StudentID}\n" +
                    $"学生电邮：{操作之2.StudentName}";

                Console.Write(输出之);

            }

        }
    }

    [DataContract(Namespace ="demo",Name ="stu_data")]
    public class 学生表1
    {
        [DataMember(Name ="stu_id")]
        public long ID { get; set; }
        [DataMember(Name ="stu_name")]
        public string Name { get; set; }
        [DataMember(Name ="stu_email")]
        public string Email { get; set; }
    }

    [DataContract(Namespace ="demo",Name ="stu_data")]
    public class 学生表2
    {
        [DataMember(Name ="stu_id")]
        public long StudentID { get; set; }
        [DataMember(Name ="stu_name")]
        public string StudentName { get; set; }
        [DataMember(Name ="stu_e_mail")]
        public string StudentEmail { get; set; }
    }
}
